package Project1;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.time.LocalDateTime;
import java.util.*;

public class Business {


    private  static final ArrayList<Orders> arrayList = new ArrayList<>();
    public static void readXMLBusiness () throws DocumentException {

        //先创建XML中的文件内容
        //1.创建解析器
        SAXReader saxReader = new SAXReader();
        //再获取根目录
        Document document = saxReader.read("D:\\software\\code\\2023_java\\2023_java_code\\code_24_5_5\\src\\Project1\\orders.xml");
        Element rootElement = document.getRootElement();
        //获取子目录
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            //对每个获取的订单进行解析各种成分
            //1.id号
            Element idElement = element.element("id");
            String id = idElement.getText();
            //类型转换
            int idInt = Integer.parseInt(id);

            //2.name
            Element nameElement = element.element("name");
            String name = nameElement.getText();

            //3.时间
            Element timeElement = element.element("LocalDateTime");
            String Datetime = timeElement.getText();
            //需要进行将其拆分
            String[] strings = Datetime.split(" ");
            //日期
            String date = strings[0];
            String[] dates = date.split("-");
            int year = Integer.parseInt(dates[0]);
            int month = Integer.parseInt(dates[1]);
            int day = Integer.parseInt(dates[2]);
            //时间
            String time = strings[1];
            String[] times = time.split(":");
            int hour = Integer.parseInt(times[0]);
            int minute = Integer.parseInt(times[1]);

            Element priceElement = element.element("price");
            Double price = Double.valueOf(priceElement.getText());

            //接着创建订单对象
            Orders orders = new Orders(idInt,name, LocalDateTime.of(year,month,day,hour,minute),price);
            System.out.println(orders);
            System.out.println("===============");
            //然后放到集合中
            arrayList.add(orders);
        }

    }

    public static void writeTodayBeforeOrderBusiness() throws Exception {
        PrintStream printStream = new PrintStream(new FileOutputStream("D:\\software\\code\\2023_java\\2023_java_code\\code_24_5_5\\src\\Project1\\text",true));

        //将集合中的今天的订单输出在文件中
        printStream.println("将集合中的今天: " + LocalDateTime.now().getYear() + " : " +LocalDateTime.now().getMonth() + " : " + LocalDateTime.now().getDayOfWeek()  + " 订单输出在文件中");
        for (Orders orders : arrayList) {
            if ( orders.getTime().getYear() <= LocalDateTime.now().getYear() && orders.getTime().getDayOfYear() <= LocalDateTime.now().getDayOfYear()){
                //将其订单输出在文件中
                System.out.println(orders);
                printStream.println(orders);
                System.out.println("成功写入!!!");
            }
        }
        printStream.println("===================================");
        printStream.close();
    }


    public static void expensivePrice() throws FileNotFoundException {
        PrintStream printStream = new PrintStream(new FileOutputStream("D:\\software\\code\\2023_java\\2023_java_code\\code_24_5_5\\src\\Project1\\text",true));
        //先找到最贵的价格是多少
        double max = 0;
        for (Orders orders : arrayList) {
            if (orders.getPrice() > max){
                max = orders.getPrice();
            }
        }

        //找到了最贵的价格
        System.out.println("最贵的订单价格为: " + max);
        //将其输出到文件中
        for (Orders orders : arrayList) {
            if (orders.getPrice() == max){
                printStream.println("=========== 最贵的订单 ==============");
                printStream.println(orders);
                printStream.println("===================================");
            }
        }

        printStream.close();

    }

    public static void sort() throws FileNotFoundException {
        PrintStream printStream = new PrintStream(new FileOutputStream("D:\\software\\code\\2023_java\\2023_java_code\\code_24_5_5\\src\\Project1\\text",true));
        arrayList.sort(new Comparator<Orders>() {
            @Override
            public int compare(Orders o1, Orders o2) {
                return (int)(o1.getPrice() - o2.getPrice());
            }
        });
        printStream.println("=======按照价格从小到大进行排序:=======");
        for (Orders orders : arrayList) {
            printStream.println(orders);
        }
        printStream.println("===================================");
        printStream.close();
    }

}
